<?php
/*
*后台页面展示
*/
namespace app\admin\controller;
use think\Controller;
use think\Request;
use \think\Db;
use app\admin\lib\Tree;
class Homerole extends Base{
     //后台首页展示
     public function index(){

        return $this->fetch();
     }
     //角色列表
     public function role_list(){
        $db = Db::name('home_role');
        $member = Db::name('member');
        $list = $db->select();
        foreach ($list as $key => $value) {
          $data['role_id'] = $value['id'];
          $user = $member->where($data)->select();
            $arr[] = [
                  'id'=>$value['id'],
                  'name'=>$value['name'],
                  'num'=>count($user),
                  'addtime'=>$value['addtime'],

            ];


        }



        $this->assign("list",$arr);

        // dump($list);
        return $this->fetch();
     }

     //添加角色试视图
     public function role_detail(){
       $request = Request::instance();//getid
       $getid = $request->route();
       $db = Db::name('home_role');
       // var_dump($getid);
       if (isset($getid['role_id'])) {
         $list = $db->where(['id'=>$getid['role_id']])->find();
         $this->assign("list",$list);
       }
       return $this->fetch();
     }
     //修改/添加角色
     public function edit_role(){
       $db = Db::name('home_role');
       $data['name'] = $_POST['name'];
       $data['addtime'] = time();
       if(isset($_POST['id'])){
         $ok = $db->where(['id'=>$_POST['id']])->update($data);
       }else{
         $ok = $db->insert($data);
       }
       if ($ok) {
        $this->success('操作成功','homerole/role_list');
      }else{
        $this->error('操作失败','homerole/role_list');
      }
    }
     //删除
    public function delete_role(){
       $db = Db::name('home_role');
       $request = Request::instance();//getid
       $getid = $request->route();
       $ok = $db->delete($getid['id']);
       if ($ok) {
        $this->success('操作成功');
        }else{
          $this->error('操作失败');
        }
     }
     //权限列表
    public function permission_list(){
        $db = Db::name('home_permission');
        $list1 = $db->select();
        $lists =  Tree::findmenu($list1,'---');
        $this->assign("list",$lists);
        return $this->fetch();
     }
    //异步修改自定信息
    public function upname(){
        $db = Db::name('home_permission');
        $values = $_POST['values'];
        $name = $_POST['name'];
        $id = $_POST['id'];
        // dump($_POST);die;
        $ok = $db->where(['id'=>$id])->update([$values=>$name,'addtime'=>time()]);
        if ($ok) {
            $arr = ['ok'=>'操作成功'];
            echo json_encode($arr);
        }else{
            $arr = ['ok'=>'操作失败'];
            echo json_encode($arr);
        }
    }

    //异步修改
    public function delete_permission(){
      $db = Db::name('home_permission');
      $request = Request::instance();//getid
      $getid = $request->route();
      if (isset($getid['id'])) {
          $db->delete($getid['id']);
          $this->success('删除成功');
      }else{
          $this->error('删除失败');
      }
    }
    // 添加数据
    public function add_permission(){
      $db = Db::name('home_permission');
      $request = Request::instance();//getid
      $getid = $request->route();
      if (isset($getid['id'])) {
          $data['name'] = '添加控制器名称';
          $data['val'] = '添加控制器方法';
          $data['category'] = '所属类型';
          $data['addtime'] = time();
          $data['uid'] = $getid['id'];
          $ok = $db->insert($data);
          if($ok){
              $this->success('操作成功');
          }else{
              $this->error('操作失败');
          }
      }else{
          $this->error('操作失败');
      }
    }
    //用户列表
    public function user_list(){
      $db = Db::name('member');
      $role = Db::name('home_role');
      $rolelist = $role->select();
      $list = $db->select();
      $this->assign("list",$list);
      $this->assign("role",$rolelist);
      return $this->fetch();
    }
    //异步设置权限分类‘
    public function upuserrole(){
      $db = Db::name('member');
      if (isset($_POST['role_id'])) {
        $data['role_id'] = $_POST['role_id'];
        $ok = $db->where(['id'=>$_POST['id']])->update($data);
        if ($ok) {
            $arr = ['ok'=>'操作成功'];
        }else{
            $arr = ['ok'=>'操作失败'];
        }
      }else{
            $arr = ['ok'=>'操作失败'];
      }
      echo json_encode($arr);
    }
    //权限列表
    public function role_permission(){
      $db = Db::name('home_permission');
      $role = Db::name('home_role');
      $list= $db->select();

      $request = Request::instance();//getid
      $getid = $request->route();

      $listd = $role->where(['id'=>$getid['role_id']])->find();
        $json = json_decode($listd['content'],true);
        $val = $this->select_role($json);
      foreach ($list as $key => $value) {

              if(isset($val[$value['id']])){
                  $values = 'checked="checked"';
              }else{
                  $values = 0;
              }
            $arr[] = [
                  'id'=>$value['id'],
                  'uid'=>$value['uid'],
                  'name'=>$value['name'],
                  'val'=>$values
            ];
      }
      $lists =  Tree::findnews($arr);
      $this->assign("id",$getid['role_id']);
      $this->assign("list",$lists);
      return $this->fetch();
    }

    //获取选择的id
    public function select_role($arr){
      if (isset($arr)) {
        foreach ($arr as $key => $value) {
            $namm[$value['id']] = $value['name'];
        }
        return $namm;
      }

    }
    //提交权限
    public function add_role(){
      $db = Db::name('home_permission');
      $role = Db::name('home_role');
      $arrid = $_POST['all'];
      $id = '';
      foreach ($arrid as $key => $value) {
        $id.= $value.',';
      }
      $arr['id'] = ['in',$id];
      $list = $db->where($arr)->field('id,name,val,pic')->select();
      $json = json_encode($list);
      $ok = $role->where(['id'=>$_POST['id']])->update(['content'=>$json]);
      if($ok){
        $this->success('操作成功',url('homerole/role_list'));
      }else{
      $this->error('操作失败');
      }

    }




















}
